package net.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.esframework.core.IRapidPassiveEngine;

import net.example.manager.Manager;
import net.example.model.ChangePasswordResult;
import net.example.ggtalk.R;
import net.example.utils.IntentUtils;
import net.example.utils.ToastUtils;

public class ChangePasswordActivity extends Activity {

    private EditText et_oldPwd;
    private EditText et_newPwd;
    private EditText et_newPwd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        ((ImageView)findViewById(R.id.p_oldPwd).findViewById(R.id.iv_ico)).setImageResource(R.drawable.pass_icon);
        ((ImageView)findViewById(R.id.p_newPwd).findViewById(R.id.iv_ico)).setImageResource(R.drawable.pass_icon);
        ((ImageView)findViewById(R.id.p_newPwd2).findViewById(R.id.iv_ico)).setImageResource(R.drawable.pass_icon);
        ((TextView)findViewById(R.id.p_oldPwd).findViewById(R.id.tv_tips)).setText("旧密码：");
        ((TextView)findViewById(R.id.p_newPwd).findViewById(R.id.tv_tips)).setText("新密码：");
        ((TextView)findViewById(R.id.p_newPwd2).findViewById(R.id.tv_tips)).setText("确认新密码：");
        this.et_oldPwd = (EditText) findViewById(R.id.p_oldPwd).findViewById(R.id.et_input);
        this.et_newPwd =(EditText)findViewById(R.id.p_newPwd).findViewById(R.id.et_input);
        this.et_newPwd2=(EditText)findViewById(R.id.p_newPwd2).findViewById(R.id.et_input);
        this.setEditText_Pwd(this.et_oldPwd);
        this.setEditText_Pwd(this.et_newPwd);
        this.setEditText_Pwd(this.et_newPwd2);
    }

    private void setEditText_Pwd(EditText editText)
    {
        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//设置为密码格式显示 *
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});//最多输入12位字符
    }


    //点击修改密码
    public void onSubmit(View view)
    {
        String oldPwd =this.et_oldPwd.getText().toString().trim();
        if (StringHelper.isNullOrEmpty(oldPwd))
        {
            ToastUtils.showShort(this, "旧密码不能为空！");
            return;
        }
        String newPwd = this.et_newPwd.getText().toString();
        if (StringHelper.isNullOrEmpty(newPwd))
        {
            ToastUtils.showShort(this, "新密码不能为空！");
            return;
        }
        String newPwd2 = this.et_newPwd2.getText().toString();
        if (StringHelper.isNullOrEmpty(newPwd2))
        {
            ToastUtils.showShort(this, "确认密码不能为空！");
            return;
        }
        if(!newPwd.equals(newPwd2))
        {
            ToastUtils.showShort(this, "二次输入的密码不一致！");
            return;
        }
        try {
            ChangePasswordResult result = Manager.getInstance().changeMyPassword(StringHelper.md5(oldPwd),StringHelper.md5(newPwd));
            switch (result)
            {
                case OldPasswordWrong:
                    ToastUtils.showShort(this, "旧密码错误！");
                    return;
                case UserNotExist:
                    ToastUtils.showShort(this, "用户不存在！");
                    return;
                case Error:
                    ToastUtils.showShort(this, "内部出现错误！");
                    return;
                default:
                    break;
            }
            ToastUtils.showShort(this, "修改成功！");
            setResult(RESULT_OK);
            finish();
        }catch (Exception e)
        {
            e.printStackTrace();
            ToastUtils.showShort(this, "内部出现错误！");
            return;
        }

    }

    //点击返回
    public void finish(View view)
    {
        setResult(RESULT_CANCELED);
        finish();
    }
}
